﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;


namespace CompanyManagement.Controllers
{
    public class TaskController : Controller
    {
        //
        // GET: /Task/

        TaskDLL dllTask = new TaskDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(TaskController));

        #region Page View

        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            Utility.setCookiValue();
            List<TaskMetaData> userlist = new List<TaskMetaData>();
            try
            {
                userlist = dllTask.GetTaskList();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(userlist);
        }

        public ActionResult Create()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            TaskMetaData TaskMetaData = new TaskMetaData();
            try
            {
                TaskMetaData = dllTask.GetTaskByID(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(TaskMetaData);
        }

        #endregion

        #region DML Operation

        [HttpPost]
        public ActionResult Create(TaskMetaData taskmetadata)
        {
            TaskMetaData TaskMetaData = new TaskMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    taskmetadata.CreatedBy = Utility.getUserID();
                    dllTask.InsertTask(taskmetadata);
                }
                else
                {
                    return View(taskmetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(taskmetadata);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(TaskMetaData taskmetadata)
        {
            TaskMetaData TaskMetaData = new TaskMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllTask.UpdateTask(taskmetadata);
                }
                else
                {
                    return View(taskmetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(taskmetadata);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            try
            {
                dllTask.DeleteTask(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return RedirectToAction("Index");
        }
        #endregion

    }
}
